package com.ktjy.t382.ch02.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.t382.ch02.entity.SysUser;
import com.ktjy.t382.ch02.entity.vo.R;
import com.ktjy.t382.ch02.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/selectList")
    public R selectList(@RequestBody SysUser sysUser, int current, int size) {
        return R.ok(userService.selectList(sysUser, current, size));
    }

    @PostMapping("/add")
    public R add(@RequestBody SysUser sysUser) {
        int add = userService.add(sysUser);
        if (add > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @PostMapping("/update")
    public R update(@RequestBody SysUser sysUser) {
        int count = 0;
        // synchronized (this) {
        SysUser user = userService.getId(sysUser.getId().intValue());
        user.setAccount(sysUser.getAccount());
        count = userService.update(user);
        if (count > 0) {
            return R.ok(user);
        } else {
            return R.fail(user);
        }
        //  }
    }
}
